package com.cn.service.impl;

import com.cn.common.Result;
import com.cn.dao.TestDeptDao;
import com.cn.entity.TestEmp;
import com.cn.service.TestDeptService;
import com.cn.tree.TreeData;
import com.cn.vo.DeptUserVo;
import com.cn.vo.TreeDept;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 部门表(TestDept)表服务实现类
 *
 * @author makejava
 * @since 2023-02-05 12:33:07
 */
@Service("testDeptService")
public class TestDeptServiceImpl implements TestDeptService {
    @Resource
    private TestDeptDao testDeptDao;


    @Override
    public Result selectByIdDept(String id) {
        DeptUserVo deptUserVo =testDeptDao.selectByIdDept(id);

        //1.构建数据
        ArrayList<TreeData> treeDepts = new ArrayList<>();

        //2.查询顶层数据
        String deptName = deptUserVo.getDeptName();

        //3.查询底层数据
        List<TestEmp> emp = deptUserVo.getEmp();

        //4.构建第一层数据
        TreeData treeDataOne = new TreeData();
        treeDataOne.setName(deptName);
        treeDataOne.setUid(deptUserVo.getId().toString());

        //5.创建第一层数据返回
        ArrayList<TreeData> treeDataListOne = new ArrayList<>();


        //6.便利数据类型
        for (TestEmp testEmp : deptUserVo.getEmp()) {

            //1.创建treeData 第二层数据
            TreeData treeDataTwo = new TreeData();

            //2.创建ListtreeDate 第二层返回
            ArrayList<TreeData> treeDataListTwo = new ArrayList<>();

            //3.设置第二层内容
            treeDataTwo.setName(testEmp.getName());
            treeDataTwo.setUid(testEmp.getDept());



            //遍历第三层数据
            for (TestEmp empTree : deptUserVo.getEmp()) {
                TreeData treeDataTree = new TreeData();
                treeDataTree.setName(deptName);
                treeDataTree.setUid(empTree.getDept());

                //便利第四层数据方式
                ArrayList<TreeData> treeDataListFour = new ArrayList<>();


                //设置第四层数据
                for (TestEmp empFour : deptUserVo.getEmp()) {
                    TreeData treeDataFour = new TreeData();
                    treeDataFour.setUid(empFour.getId().toString());
                    treeDataFour.setName(empFour.getName());
                    treeDataListFour.add(treeDataFour);
                    treeDataTree.setTreeData(treeDataListFour);
                }

                treeDataListTwo.add(treeDataTree);
                treeDataTwo.setTreeData(treeDataListTwo);
            }
            treeDataListOne.add(treeDataTwo);
            treeDataOne.setTreeData(treeDataListTwo);
        }
        treeDepts.add(treeDataOne);
        return new Result(200,"成功",treeDepts);
    }
}
